package com.fce.fcesystem.controller;

import com.fce.fcesystem.pojo.Menu;
import com.fce.fcesystem.pojo.Role;
import com.fce.fcesystem.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.fce.fcesystem.service.serviceImp.UserServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 视图解析进行页面跳转
 */
@Controller
public class IndexController {

    public static  Map<String, String> currencyMap = new HashMap<String, String>();

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("index")
    public String index(Model model) {
        Subject subject = SecurityUtils.getSubject();
        Object principal = subject.getPrincipal();
        User rolesByAccount = userService.findRolesByAccount((String) principal);
        List<Role> roles = rolesByAccount.getRoles();
        List<Menu> menuList =new ArrayList<>();
        if (roles != null && !roles.isEmpty()){
           menuList = userService.loadMenu(roles.get(0).getId());
        }
        model.addAttribute("menusList",menuList);
        return "basePage";
    }

    @RequestMapping("data")
    public String getEchartsData() {
        return "index";
    }

    @RequestMapping("login")
    public String login() {
        return "web/login";
    }

    @RequestMapping("logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "web/login";
    }

    @RequestMapping("register")
    public String register() {
        return "web/register";
    }

    @RequestMapping("userlist")
    public String userlist(Model model) {
        List<Role> roles = userService.getAllRoles();
        model.addAttribute("roles",roles);
        return "userlist";
    }

    @RequestMapping("transaction")
    public String transactionList(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "transaction";
    }

    @RequestMapping("personinfo")
    public String personinfo() {
        return "personinfo";
    }


    @RequestMapping("userset")
    public ModelAndView userset(String userName) {
        ModelAndView modelAndView = new ModelAndView();
        User user = userService.selectUserByAccount(userName);
        modelAndView.addObject("user", user);
        modelAndView.setViewName("personinfo");
        return modelAndView;
    }

    @RequestMapping("updateUserPassword")
    public String updateUserPassword() {
        return "web/updateUserPassword";
    }

    @RequestMapping("dolist")
    public String dolist() {
        return "web/dolist";
    }

    @RequestMapping("getPassword")
    public String getPassword() {
        return "web/getPassword";
    }

    @RequestMapping("quotePrice")
    public String quotePrice(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "quotePrice";
    }

    @RequestMapping("customer")
    public String customerList(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "customerList";
    }

    @RequestMapping("account")
    public String accountingList(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "accountList";
    }

    @RequestMapping("currencyPair")
    public String currencyList(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "currencyPairList";
    }

    @RequestMapping("quoteSource")
    public String quoteSource(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "quoteSourceList";
    }

    @RequestMapping("systemParam")
    public String systemParamList() {
        return "systemParamList";
    }

    @RequestMapping("position")
    public String positionList(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "positionList";
    }

    @RequestMapping("accumulate")
    public String accumulateList(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "accumulateList";
    }

    @RequestMapping("pending")
    public String pendingList() {
        return "pendingList";
    }

    @RequestMapping("loginByNote")
    public String loginByNote() {
        return "SMSLogin";
    }

    @RequestMapping("getCurrency")
    public Model success(Model model) {

        model.addAttribute("currenyMap",currencyMap);

        return model;
    }

    @RequestMapping("qualification")
    public String customerQualification() {
        return "qualificationList";
    }

    @RequestMapping("contract")
    public String createContract(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "contractTransaction";
    }

    @RequestMapping("contractList")
    public String contractList(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "contractTransactionList";
    }

    @RequestMapping("option")
    public String createOption(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "optionTransaction";
    }

    @RequestMapping("optionList")
    public String createOptionList(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "optionTransactionList";
    }

    @RequestMapping("permissionList")
    public String permissionList(Model model) {
        List<Menu> menus = userService.getAllMenus();
        model.addAttribute("menus",menus);
        return "permissionList";
    }

    @RequestMapping("settlementList")
    public String settlementList(Model model) {
        model.addAttribute("currenyMap",currencyMap);
        return "settlementList";
    }
}

